<template>
  <!-- 合同变更 -->
  <div class="app-container-add">
    <el-row :gutter="10" class="button-wrap">
      <el-col :span="1.5">
        <el-button icon="el-icon-arrow-left" size="mini" @click="handleReturn()">返回</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button v-clickForbidden icon="el-icon-document-checked" size="mini" @click="handleSave">{{ flag ? '保存' : '变更' }}</el-button>
      </el-col>
    </el-row>
    <div ref="mainCont" class="main-content">
      <el-row :gutter="30">
        <el-col :span="17">
          <el-card class="box-card" shadow="never">
            <div slot="header" class="clearfix">
              <span v-if="flag">合同编辑</span>
              <span v-if="!flag">合同变更</span>
            </div>
            <el-form ref="form" :model="formData" :rules="rules" label-width="100px">
              <el-row :gutter="20">
                <el-col :lg="12" :xl="8">
                  <el-form-item label="合同名称" prop="contractName">
                    <el-input v-model="formData.contractName" clearable size="medium" />
                  </el-form-item>
                </el-col>
                <el-col :lg="12" :xl="8">
                  <el-form-item label="建设单位" prop="construction">
                    <el-input v-model="formData.construction" clearable size="medium" />
                  </el-form-item>
                </el-col>
                <!--                <el-col :lg="12" :xl="8">-->
                <!--                  <el-form-item label="合同类型" prop="contractType">-->
                <!--                    <el-select v-model="formData.contractType" clearable style="width:100%" size="medium" placeholder="">-->
                <!--                      <el-option-->
                <!--                        v-for="item in contractType"-->
                <!--                        :key="item.value"-->
                <!--                        :label="item.label"-->
                <!--                        :value="item.value"-->
                <!--                      />-->
                <!--                    </el-select>-->
                <!--                  </el-form-item>-->
                <!--                </el-col>-->
                <el-col :lg="12" :xl="8">
                  <el-form-item label="合同工期" prop="contractDuration">
                    <el-input v-model="formData.contractDuration" clearable size="medium">
                      <template slot="append">天</template>
                    </el-input>
                  </el-form-item>
                </el-col>
                <el-col :lg="12" :xl="8">
                  <el-form-item label="合同单价" prop="contractPriceName">
                    <el-input v-model="formData.contractPriceName" clearable size="medium" @input="contractPrice($event)">
                      <template slot="append">元</template>
                    </el-input>
                  </el-form-item>
                </el-col>
                <el-col :lg="12" :xl="8">
                  <el-form-item label="合同总金额" prop="contractAmountName">
                    <el-input v-model="formData.contractAmountName" clearable size="medium" @input="contractAmount($event)">
                      <template slot="append">万元</template>
                    </el-input>
                  </el-form-item>
                </el-col>
                <el-col :lg="12" :xl="8">
                  <el-form-item label="钻孔总数" prop="drillingCount">
                    <el-input v-model="formData.drillingCount" clearable size="medium">
                      <template slot="append"><el-button />个</template>
                    </el-input>
                  </el-form-item>
                </el-col>
                <el-col :lg="12" :xl="8">
                  <el-form-item label="钻孔总米数" prop="drillMember">
                    <el-input v-model="formData.drillMember" clearable size="medium">
                      <template slot="append"><el-button />m</template>
                    </el-input></el-form-item>
                </el-col>
                <el-col :lg="12" :xl="8">
                  <el-form-item label="最小深度" prop="minScope">
                    <el-input v-model="formData.minScope" clearable size="medium" placeholder="">
                      <template slot="append"><el-button />m</template>
                    </el-input>
                  </el-form-item>
                </el-col>
                <el-col :lg="12" :xl="8">
                  <el-form-item label="最大深度" prop="maxScope">
                    <el-input v-model="formData.maxScope" clearable size="medium" placeholder="">
                      <template slot="append"><el-button />m</template>
                    </el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="24">
                  <el-form-item label="合同附件" prop="appFiles">
                    <FileUplode
                      ref="fileUplode"
                      :edit-file-list="conractFileList"
                      @getFileList="getContractFile"
                    />
                  </el-form-item>
                </el-col>
              </el-row>
            </el-form>
          </el-card>
        </el-col>
        <el-col :span="7">
          <el-card class="box-card" shadow="never">
            <div slot="header" class="clearfix">
              <span>变更记录</span>
            </div>
            <div v-if="!flag" class="block">
              <el-timeline>
                <el-timeline-item v-for="item in tableData" :key="item.id" :timestamp="item.createTime" placement="top">
                  <el-card>
                    <p @click="handleRecord(item.id)"><i class="el-icon-zoom-in" :class="['look']" />{{ item.createName }} 变更于 {{ item.createTime }}</p>
                  </el-card>
                </el-timeline-item>
              </el-timeline>
            </div>
          </el-card>
        </el-col>
      </el-row>
      <contractRecord ref="contractRecord" />
    </div>
  </div>

</template>

<script>
// 字段校验
import { integer, validateDouble } from '@/utils/validate'
import contractRecord from './contractRecord'
// 上传文件
import FileUplode from '@/components/Upload/FileUplode'
import { updateContract, listContract, getContractById } from '@/api/projectManage/project/proContract'
export default {
  name: 'DetailDialog',
  components: {
    contractRecord,
    FileUplode
  },
  props: {
    flag: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      // 变更记录
      tableData: [],
      // 合同列表
      conractFileList: [],
      // 合同类型
      contractType: [
        {
          value: '0',
          label: '补充合同'
        },
        {
          value: '1',
          label: '原合同'
        }
      ],
      /* 新增 start */
      dialogTitle: '', // 对话框是否弹出显示
      dialogVisible: false, // 对话框是否弹出显示
      formData: {
        id: '',
        proId: '', // 项目id
        contractName: '', // 合同名称
        construction: '', // 建设单位
        contractPriceName: '',
        contractPrice: '', // 合同单价
        contractDuration: '', // 合同工期
        contractType: '0', // 合同类型
        contractAmountName: '', // 合同总金额
        contractAmount: '', // 合同总金额
        drillingCount: '', // 钻孔总个数
        drillMember: '', // 钻孔总米数
        minScope: '', //  勘探最小深度
        maxScope: '', // 勘探最大深度
        appFiles: '' // 合同附件
      },
      rules: {
        contractName: [{ required: true, message: '合同名称不能为空', trigger: 'blur' }],
        construction: [{ required: true, message: '建设单位不能为空', trigger: 'blur' }],
        contractPriceName: [
          { required: true, message: '合同单价不能为空', trigger: 'blur' }
          // { validator: validateDouble, trigger: 'blur' }
        ],
        contractDuration: [
          { required: true, message: '合同工期不能为空', trigger: 'blur' },
          { validator: integer, trigger: 'blur' }
        ],
        contractType: [{ required: true, message: '合同类型不能为空', trigger: 'blur' }],
        contractAmountName: [
          { required: true, message: '合同总金额不能为空', trigger: 'blur' }
          // { required: true, validator: validAmountCheck, trigger: 'blur' }
        ],
        drillingCount: [
          { required: true, message: '钻孔总个数不能为空', trigger: 'blur' },
          { validator: integer, trigger: 'blur' }
        ],
        drillMember: [
          { required: true, message: '钻孔总米数不能为空', trigger: 'blur' },
          { validator: validateDouble, trigger: 'blur' }
        ],
        minScope: [
          { required: true, message: '勘探最小深度不能为空', trigger: 'blur' },
          { validator: validateDouble, trigger: 'blur' }
        ],
        maxScope: [
          { required: true, message: '勘探最大深度不能为空', trigger: 'blur' },
          { validator: validateDouble, trigger: 'blur' }
        ],
        appFiles: [{ required: true, message: '合同附件不能为空', trigger: 'blur' }]
      }
      /* 新增 end */
    }
  },
  created() {
    const { proId } = this.$route.query
    this.getDetailById(proId)
    this.getList(proId)
  },
  methods: {
    // 合同总金额
    contractAmount(e) {
      if (e) {
        this.$set(this.formData, 'contractAmountName', this.divideMoney(e, 6))
        this.$set(this.formData, 'contractAmount', e * 10000)
        this.$forceUpdate()
      }
    },
    // 合同单价
    contractPrice(e) {
      if (e) {
        this.$set(this.formData, 'contractPriceName', this.divideMoney(e, 2))
        this.$set(this.formData, 'contractPrice', e)
        this.$forceUpdate()
      }
    },
    getList(proId) {
      listContract({ proId }).then(res => {
        this.tableData = res.data.rows
      })
    },
    // 获取详情
    async getDetailById(proId) {
      const { data } = await getContractById(proId)
      if (data) {
        this.formData = { ...data }
        this.formData.contractAmountName = this.divideMoney(data.contractAmount, 6, true)
        this.formData.contractPriceName = this.divideMoney(data.contractPrice, 2)
        if (data.appFiles && data.appFiles.length > 0) {
          this.conractFileList = data.appFiles
        }
      }
    },
    // 查看合同记录
    handleRecord(id) {
      this.$refs.contractRecord.handleAdd('变更记录', id)
    },
    // 保存
    handleSave() {
      this.$refs['form'].validate(async(valid) => {
        if (valid) {
          this.btnDisabled = true
          // 变更合同
          await updateContract(this.formData)
          this.msgSuccess('保存成功')
          this.handleReturn()
        }
      })
    },
    // 返回
    handleReturn() {
      this.$router.go(-1)
    },
    // 获取合同附件
    getContractFile(fileList, deleteFileIds) {
      this.formData.appFiles = fileList
      this.formData.fileDelete = deleteFileIds
    }
  }
}
</script>

<style lang='scss' scoped>
.look {
  font-size: 16px;
  cursor: pointer;
  margin-right: 5px;
  vertical-align: middle;
  color: rgb(0, 119, 255);
}
</style>
